<template>
  <div class="login_container">
    <div class="login_box">
      <div class="logo">
        <img src="https://gitee.com/zj095/cloudimg/raw/master/202110230931189.png" alt="">
      </div>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        label-width="100px"
        class="login_form"
        :rules="loginFormRules"
      >
        <!-- 账号 -->
        <el-form-item prop="account" label="账号">
          <el-input
            v-model="loginForm.account"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password" label="密码">
          <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
          ></el-input>
          </el-form-item>
        <!-- 名字 -->
        <el-form-item prop="name" label="名字">
          <el-input
            v-model="loginForm.name"
            placeholder="请输入名字"
          ></el-input>
          </el-form-item>
          <!-- 邮箱 -->
           <el-form-item prop="email" label="邮箱">
          <el-input
            v-model="loginForm.email"
            placeholder="请输入邮箱"
          ></el-input>
          </el-form-item>
          <!-- 电话 -->
           <el-form-item prop="phone" label="电话">
          <el-input
            v-model="loginForm.phone"
            placeholder="请输入电话"
          ></el-input>
          </el-form-item>
          
        <!-- 按钮区域 -->
        <el-form-item class="btn_1">
          <el-button
          @click="cancel"
          >取消</el-button>

          <el-button
            type="primary"
            @click="login"
          >注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
       // 验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/

      if (regEmail.test(value)) {
        // 合法邮箱
        return cb()
      }

      cb(new Error('请输入合法的邮箱'))
    }
    // 验证手机的规则
    var checkMobile = (rule, value, cb) => {
      const regMobile =
        /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

      if (regMobile.test(value)) {
        // 合法手机号
        return cb()
      }

      cb(new Error('请输入合法的手机号'))
    }
    return {
      loginForm: {
        account: "",
        password: "",
        name: "",
        email: "",
        phone: ""
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入名字', trigger: 'blur' },
        ],
         email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ]
      }
    };
  },

  methods: {
    async login() {
      //this.$router.push('/login');
    //  const {data: res } = await this.$http.post('http://gkd.aztop.cn/api/user/registeredss',this.loginForm)
    const {data: res} = await this.$http.post('/api/user/registeredss',this.loginForm)
    console.log(res);
    if(res.code == 200){
      this.$message.success("注册成功！")
      this.$router.push('/login')
    }
    if(res.code == 100){
      this.$message.error("注册失败！")
    }
    },
    cancel() {
        this.$router.push('/login');
    }
  },
};
</script>

<style scoped>
.login_container {
  background: url("https://gitee.com/zj095/cloudimg/raw/master/202110230933373.png");
  height: 100%;
}

.logo {
    position: absolute;
    left: 6vw;
    top: -1vw;
}
.login_form {
    position: absolute;
    /* left: 2vw; */
    top: 7vw;
}

 .login_box {
  width: 370px;
  height: 500px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>